using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class DialoguePanel : BasePanel
{
    private Image UIAvatar;
    private Transform UINameBox;
    private TextMeshProUGUI UIName;
    private TextMeshProUGUI UIContent;
    private Transform UISelects;

    public GameObject DialogueOptionPrefab;

    public float TextSpeed = 0.1f;

    private List<DialogueLine> dialogueList;
    private int index; // 索引
    private bool isTyping = false; // 正在打字
    private bool hasOptions = false; // 是否有选项要选择
    private float _textSpeed; //开始逐字打印速度

    override protected void Awake()
    {
        base.Awake();
        InitUI();
        _textSpeed = TextSpeed;
    }

    private void InitUI()
    {
        UIAvatar = transform.Find("Avatar").GetComponent<Image>();
        UINameBox = transform.Find("NameBox");
        UIName = transform.Find("NameBox/Name").GetComponent<TextMeshProUGUI>();
        UIContent = transform.Find("ContentBox/Content").GetComponent<TextMeshProUGUI>();
        UISelects = transform.Find("Selects");

        UIAvatar.gameObject.SetActive(false);
        UINameBox.gameObject.SetActive(false);
    }

    public void ShowDialogue(List<DialogueLine> dialogueList)
    {
        if (dialogueList == null)
        {
            ClosePanel();
            return;
        }

        if (isTyping)
        {
            return;
        }

        this.dialogueList = dialogueList;

        Play();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            // 如果正在打字， 再次按下R吗快速显示所有对话
            if (isTyping)
            {
                _textSpeed = 0;
            }
            else if (!isTyping && !hasOptions)
            {
                _textSpeed = TextSpeed;
                if (index >= dialogueList.Count)
                {
                    // 对话已结束
                    ClosePanel();
                    return;
                }

                // 播放下一个动画
                Play();
            }
        }
    }

    private void Play()
    {
        DialogueLine dialogueLine = dialogueList[index++];
        Debug.Log($"Play dialogue: {dialogueLine}, index: {index}");
        if (dialogueLine == null)
        {
            // 对话已结束
            ClosePanel();
            return;
        }

        // 显示头像
        if (dialogueLine.Avatar == null)
        {
            UIAvatar.gameObject.SetActive(false);
        }
        else
        {
            UIAvatar.sprite = dialogueLine.Avatar;
            UIAvatar.gameObject.SetActive(true);
        }

        // 显示名称
        if (dialogueLine.Name is null or "")
        {
            UINameBox.gameObject.SetActive(false);
        }
        else
        {
            UIName.text = dialogueLine.Name;
            UINameBox.gameObject.SetActive(true);
        }

        // 显示文本
        StartCoroutine(SetTextUI(dialogueLine));

        // 先清空选项
        // 这里可以考虑初始化一批按钮, 通过Active来控制, 减少对象创建次数
        for (int i = 0; i < UISelects.childCount; i++)
        {
            Destroy(UISelects.GetChild(i).gameObject);
        }

        // 创建选项
        // if (dialogueLine.Options == null)
        // {
        //     hasOptions = false;
        // }
        // else
        // {
        //     hasOptions = true;
        //     foreach (var option in dialogueLine.Options)
        //     {
        //         var item = Instantiate(DialogueOptionPrefab.transform, UISelects);
        //         // 点击事件
        //         item.GetComponent<Button>().onClick.AddListener(() => OnOptionSelected(option));
        //         // 显示内容
        //         item.GetComponentInChildren<Text>().text = option.OptionText;
        //     }
        // }
    }

    private IEnumerator SetTextUI(DialogueLine dialogueLine)
    {
        isTyping = true;
        UIContent.text = "";
        foreach (var c in dialogueLine.Text)
        {
            UIContent.text += c;
            yield return new WaitForSeconds(_textSpeed);
        }

        isTyping = false;
    }

    private void OnOptionSelected(DialogueOption option)
    {
        if (option == null) return;

        DialogueLine selectedOption = option.NextDialogueLine;
        if (selectedOption != null)
        {
            // 显示下一个对话
            // ShowDialogue(selectedOption);
        }
    }
}